package de.clapp.musicbox.listAdapter;

import android.content.Context;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.clapp.CLAppModel;
import de.clapp.R;
import de.clapp.Settings;
import de.clapp.data.Club;
import de.clapp.data.MusicItem;
import de.clapp.musicbox.MusicModel;
import de.clapp.toolkit.ImageManager;

public class CurrentlyPlayingListAdapter extends ArrayAdapter<MusicItem> implements MusicListAdapter
{
    private final LayoutInflater inflater;

    private ListFragment listFragment;

    private MusicModel musicModel;

    public CurrentlyPlayingListAdapter(Context context, ListFragment listFragment)
    {
        super(context, R.layout.music_entry_title);

        this.musicModel = MusicModel.getI();
        this.musicModel.addAlreadyPlayedSongsObserver(this);
        this.listFragment = listFragment;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount()
    {
        return this.musicModel.getAlreadyPlayedSongs().size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;

        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.music_entry_interpret, parent, false);

            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.music_entry_title);
            holder.artist = (TextView) convertView.findViewById(R.id.music_entry_artist);
            holder.image = (ImageView) convertView.findViewById(R.id.music_entry_image);

            convertView.setTag(holder);

        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        MusicItem item = this.musicModel.getAlreadyPlayedSongs().get(position);
        if (item != null)
        {
            holder.title.setText(item.getTitel());
            holder.artist.setText(item.getInterpret() + " - " + item.getAlbum());

            String tag = "MusicImages\\" + item.getInterpret() + "-" + item.getAlbum() + ".png";
            holder.image.setTag(tag);
            Club club = CLAppModel.getInstance().getCurrentClub();
            ImageManager.getInstance().loadBitmap(tag, holder.image, 50, 50, club.getPort(), club.getIpAddress());
        }
        return convertView;
    }

    /**
     * The Class ViewHolder.
     */
    private static class ViewHolder
    {
        /** The logo image view. */
        public ImageView image;

        /** The title text view. */
        public TextView title;

        /** The content text view. */
        public TextView artist;
    }

    @Override
    public void dataChanged()
    {
        super.notifyDataSetChanged();
        this.listFragment.setListShown(true);
    }

}
